<?php

namespace common\service;
use herosphp\bean\Beans;
use herosphp\session\Session;
use herosphp\core\Loader;

class LoginService extends CommonService
{

	public function __construct()
	{
		Session::start();
	}
	public function login($account,$password)
	{
	
//		session_start();
		$rbacService = Beans::get('systemmanage.admin.service');
		$password = passwd($password);
		$roleservice = Beans::get('systemmanage.role.service');
		$admin = $rbacService->getItem(array('account' => $account,'password'=>$password));
		if(!$admin)
		{
			return "用户名或密码错误";
			//return L("LOGIN_USER_ERROR");
		}
		$roleInfo = $rbacService->getRoles($admin['id']);
		$admin['roles'] = $roleInfo;
		$priv = $roleservice->getPrivilegess($admin['id']);
		$pv2 = array();
		$pv1 = array();
		foreach($priv as $v => $k)
		{
			$pvs = explode('@',$v);
			$pv2tmp = $pvs[0] . '@'. $pvs[1];
			if(!in_array($pv2tmp,$pv2))
			{
				$pv2[$pv2tmp] = $pv2tmp;
			}
			if(!in_array($pvs[0],$pv1))
			{
				$pv1[$pvs[0]] = $pvs[0];	
			}
		}
		$priv = array_merge($priv,$pv2);
		$priv = array_merge($priv,$pv1);
		$admin['priv'] = $priv;
		$_SESSION['admin'] = $admin;
		$_SESSION['login_time'] = time(0);
		return $admin;


	}

	public function hasLogin()
	{
		//		session_start();
		if($_SESSION['admin'])
		{
			return true;
		}
		return false;
	}
	public  function logout()
	{
		session_destroy();
		return true;
	}
}
